﻿Imports System
Imports System.Collections
Imports System.Collections.Generic
Imports System.ComponentModel.Design.Serialization
Imports System.Workflow.ComponentModel

Namespace System.Workflow.ComponentModel.Compiler
    Public NotInheritable Class CodeGenerationManager
        Implements IServiceProvider
        ' Methods
        Public Sub New(ByVal serviceProvider As IServiceProvider)
            Me.serviceProvider = serviceProvider
        End Sub

        Public Function GetCodeGenerators(ByVal type As Type) As ActivityCodeGenerator()
            If (type Is Nothing) Then
                Throw New ArgumentNullException("type")
            End If
            If Me.hashOfGenerators.Contains(type) Then
                Return DirectCast(Me.hashOfGenerators.Item(type), List([Of] ActivityCodeGenerator)).ToArray
            End If
            Dim list As New List([Of] ActivityCodeGenerator)
            Dim generator As ActivityCodeGenerator
            For Each generator In ComponentDispenser.CreateComponents(type, GetType(ActivityCodeGeneratorAttribute))
                list.Add(generator)
            Next
            Me.hashOfGenerators.Item(type) = list
            Return list.ToArray
        End Function

        Public Function GetService(ByVal serviceType As Type) As Object
            If (Me.serviceProvider Is Nothing) Then
                Return Nothing
            End If
            Return Me.serviceProvider.GetService(serviceType)
        End Function


        ' Properties
        Public ReadOnly Property Context As ContextStack
            Get
                If (Me.context Is Nothing) Then
                    Me.context = New ContextStack
                End If
                Return Me.context
            End Get
        End Property


        ' Fields
        Private context As ContextStack
        Private hashOfGenerators As Hashtable = New Hashtable
        Private serviceProvider As IServiceProvider
    End Class
End Namespace

